package com.treasure.service.commonutil;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

/** 
* @ClassName    : QiNiuYun 
* @author       : panxingwu 
* @date         : 2018年11月18日 下午8:51:32 
* @Description  : 七牛云工具
*/
public class QiNiuYun {
	private static final String accessKey = "BtLoZWyvORYXUfRcEw566IRUbOPfa01BVBevGSN_";
	private static final String secretKey = "e-mSLMumUNC9L75Yn0rIpBsNrd6mjojGx9qOE2ri";
	/** 
	* @Title      :uploadBytes 
	* @param      :@param uploadBytes
	* @param      :@param bucketName  七牛云对象空间名称
	* @param      :@param fileName    文件名称
	* @return     :void 
	* @Description:字节上传
	*/
	public static String uploadBytes(String bucketName,String fileName,byte[] uploadBytes) {
		String url = "";
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		UploadManager uploadManager = new UploadManager(cfg);
		//生成上传凭证，然后准备上传
		String key = fileName;
		try {
		    Auth auth = Auth.create(accessKey, secretKey);
		    String upToken = auth.uploadToken(bucketName);
		    try {
//				BucketManager bucketMgr = new BucketManager(auth, cfg);
//				Response delete = bucketMgr.delete(bucketName, key);
//				delete.close();
		        Response response = uploadManager.put(uploadBytes, key, upToken);
		        //解析上传成功的结果
		        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
		        url = putRet.key;
		    } catch (QiniuException ex) {
		        Response r = ex.response;
		        System.err.println(r.toString());
		        try {
		            System.err.println(r.bodyString());
		        } catch (QiniuException ex2) {
		            //ignore
		        }
		    }
		} catch (Exception ex) {
		}finally {
			
		}
		return url;
	}

	/**
	* @Title      :uploadInputStream 
	* @param      :@param bucketName  空间名
	* @param      :@param filename
	* @param      :@param inputStream 文件名
	* @return     :void 
	* @Description:输入流上传
	*/
	public static String uploadInputStream(String bucketName,String filename,ByteArrayInputStream inputStream) {
		String url = "";
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		UploadManager uploadManager = new UploadManager(cfg);
		String key = filename;
		try {
		    Auth auth = Auth.create(accessKey, secretKey);
		    String upToken = auth.uploadToken(bucketName);
		    try {
		        Response response = uploadManager.put(inputStream,key,upToken,null, null);
		        //解析上传成功的结果
		        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
		        url = key;
		        System.out.println(putRet.key);
		        System.out.println(putRet.hash);
		    } catch (QiniuException ex) {
		        Response r = ex.response;
		        System.err.println(r.toString());
		        try {
		            System.err.println(r.bodyString());
		        } catch (QiniuException ex2) {
		            //ignore
		        }
		    }
		} catch (Exception ex) {
		    //ignore
		}
		return url;
	}
}
